import pynput, time
import os
import glob
import pyautogui as pg

nub = 1  # 操作重复次数
ii = []  # 点击坐标
start = time.time()  # 开始时间
# path = r'C:\Users\Administrator\Desktop\步骤记录'   #默认存储文件夹
path = os.getcwd()  # 默认存储文件夹
txt_name = 'Click.txt'  # 默认文件名
n = 0  # 默认值，不需要改动


def fielisin(file_no):  # 创建文件的方法
    if not os.path.exists(file_no):
        os.mkdir(file_no)
        print('创建文件成功')
    else:
        pass


def on_click_1(x, y, button, pressed):
    global start
    fielisin(path)
    os.chdir(path)
    if pressed:
        pass
    elif button == pynput.mouse.Button.middle:  # 中键删除最后一次操作
        ii.pop()
        ii.pop()
        print('删除动作成功')
    elif button == pynput.mouse.Button.left:  # 左键记录数据
        ii.append((x, y))
        end = time.time()  # 点击时间
        tin = end - start  # 间隔时间
        start = end
        tin = '%.2f' % tin
        ii.append(float(tin))
        print(ii)
    elif button == pynput.mouse.Button.right:  # 右键退出鼠标记录，并存储入文档等待调用
        for i in ii:
            # print(i)
            with open(txt_name, 'a', encoding='utf-8') as file:
                file.write(str(i) + "\n")
        print('保存成功')
        return False


with pynput.mouse.Listener(on_click=on_click_1, ) as listener:
    listener.join()
    print('监听完毕,是否需要继续操作，3秒后进入之前操作')
time.sleep(3)
file_names = glob.glob(path + "\*.txt")
for file_name in file_names:
    with open(file_name, 'r', encoding='utf-8') as file:  # 读取文档的步骤1
        ii = file.readlines()
        bz = []
        for i1, i2 in zip(ii[0::2], ii[1::2]):  # 读取文档的步骤2，把读取数字转化为是列表
            str2 = i1.replace('(', "").replace(')', '').replace('\n', '')
            tu = str2.split(',')
            tu2 = list(map(lambda x: int(x), tu))
            tu2 = tuple(tu2)
            tu3 = float(i2)
            bz.append(tu2)
            bz.append(tu3)
            ii = bz
        while True:  # 执行次数，所需要执行次数，不设置就是一直循环
            n += 1
            for i, j in zip(ii[0::2], ii[1::2]):
                pg.click(i)  # 点击坐标
                time.sleep(j)  # 间隔时间
            # time.sleep(180)
            if n == nub:
                break
    print('操作完成。')
